package com.hrg.rds.biz.comm;

import cn.hutool.json.JSONUtil;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.seer.sdk.rbk.RbkResult;
import com.hrg.seer.sdk.rbk.RbkResultKind;
import com.hrg.seer.sdk.rbk.api.ConfigApiEnum;
import com.hrg.seer.sdk.rbk.msg.BaseRes;
import com.hrg.seer.sdk.rbk.msg.config.ConfigDownloadMapReq;
import com.hrg.seer.sdk.rbk.msg.config.ConfigDownloadMapRes;
import com.hrg.seer.sdk.rbk.msg.config.ConfigLockReq;
import com.hrg.seer.sdk.rbk.msg.config.ConfigUnlockReq;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ConfigService {
	// 这里的接口最好是给出接口请求是否正常，返回结果是否成功由调用方判断
	private static RbkResult ROBOT_CONFIG_LOCK_REQ(String ip, String nickName) {
		int apiNo = ConfigApiEnum.ROBOT_CONFIG_LOCK_REQ.getApiNo();
		ConfigLockReq req = ConfigLockReq.builder().nick_name(nickName).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean configLock(String ip) {
		String nickName = RdsConfiguration.ControlRightsName;
		RbkResult rbkResult = ROBOT_CONFIG_LOCK_REQ(ip, nickName);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("抢占控制权成功 ip = {}, nickName = {}", ip, nickName);
				return true;
			}
			log.error("抢占控制权失败 ret_code = {}, err_msg = {}，ip = {}, nickName = {}", res.getRet_code(), res.getErr_msg(), ip, nickName);
		}
		log.error("抢占控制权失败 kind = {}，ip = {}, nickName = {}", rbkResult.getKind().name(), ip, nickName);
		return false;
	}

	private static RbkResult ROBOT_CONFIG_UNLOCK_REQ(String ip, String nickName) {
		int apiNo = ConfigApiEnum.ROBOT_CONFIG_LOCK_REQ.getApiNo();
		ConfigUnlockReq req = ConfigUnlockReq.builder().nick_name(nickName).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean configUnlock(String ip) {
		String nickName = RdsConfiguration.ControlRightsName;
		RbkResult rbkResult = ROBOT_CONFIG_UNLOCK_REQ(ip, nickName);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("释放控制权成功 ip = {}, nickName = {}", ip, nickName);
				return true;
			}
			log.error("释放控制权失败 ret_code = {}, err_msg = {}，ip = {}, nickName = {}", res.getRet_code(), res.getErr_msg(), ip, nickName);
		}
		log.error("释放控制权失败 kind = {}，ip = {}, nickName = {}", rbkResult.getKind().name(), ip, nickName);
		return false;
	}

	private static RbkResult ROBOT_CONFIG_DOWNLOADMAP_REQ(String ip, String mapName) {
		int apiNo = ConfigApiEnum.ROBOT_CONFIG_DOWNLOADMAP_REQ.getApiNo();
		ConfigDownloadMapReq req = ConfigDownloadMapReq.builder().map_name(mapName).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static ConfigDownloadMapRes configDownloadMap(String ip, String mapName) {
		RbkResult rbkResult = ROBOT_CONFIG_DOWNLOADMAP_REQ(ip, mapName);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			ConfigDownloadMapRes res = JSONUtil.toBean(rbkResult.getResStr(), ConfigDownloadMapRes.class);
			if (res.getRet_code() == 0) {
				log.info("下载地图成功 ip = {}, mapName = {}", ip, mapName);
				return res;
			}
			log.error("下载地图失败 ret_code = {}, err_msg = {}，ip = {}, mapName = {}", res.getRet_code(), res.getErr_msg(), ip, mapName);
		}
		log.error("下载地图失败 kind = {}，ip = {}, mapName = {}", rbkResult.getKind().name(), ip, mapName);
		return null;
	}



}
